  #include"stdio.h"
  #include<stdlib.h>
  //#define NULL 0
  
   struct node               /* STRUCTURE CONTANING A DATA PART AND A LINK PART */
 {
   int data;
   struct node *next;
 }*p;                /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE IN LIST*/
 int count();

    void del_node(int num)                /*THIS FUNCTION DELETES A NODE */
 {
     struct node *temp, *m;
temp=p;
      while(temp!=NULL)
    {
       if(temp->data==num)
       {
           if(temp==p)
           {
              p=temp->next;
              free(temp);
              return;
           }
           else
         {
           m->next=temp->next;
           free(temp);
           return;
         }
      }else
        {
           m=temp;
          temp= temp->next;
        }

}
    printf("ELEMENT %d NOT FOUND ", num);
}

   void append( int num )        /*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */
 {
     struct node *temp,*r;
     /* CREATING A NODE AND ASSIGNING A VALUE TO IT */

       temp= (struct node *)malloc(sizeof(struct node));
       temp->data=num;
       r=(struct node *)p;

      if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */
     {
    p=temp;
         p->next =NULL;
     }
  else
     {        /* GO TO LAST AND ADD*/

             while( r->next != NULL)
       r=r->next;
       r->next =temp;
       r=temp;
       r->next=NULL;
     }
  }

      void addbeg( int num )         /* ADD A NEW NODE AT BEGINNING  */
   {
        /*  CREATING A NODE AND INSERTING VALUE TO IT  */

   struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;

       /* IF LIST IS NULL ADD AT BEGINNING  */
        if ( p== NULL)
       {
          p=temp;
          p->next=NULL;
       }

   else
      {
          temp->next=p;
          p=temp;
      }
   }

 void addafter(int num, int loc)            /*  ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */
  {
     int i;
     struct node *temp,*t,*r;
     r=p;       /* here r stores the first location */
      if(loc > count()+1 || loc <= 0)
   {
         printf("insertion is not possible :");
            return;
   }
        if (loc == 1)/* if list is null then add at beginning */
         {
           addbeg(num);
           return;
         }
      else
 {
       for(i=1;i<loc;i++)
          {
             t=r;   /* t will be holding previous value */
             r=r->next;
          }
         temp=(struct node *)malloc(sizeof(struct node));
         temp->data=num;
         t->next=temp;
         t=temp;
         t->next=r;
        return;
       }
}

  void display(struct node *r)     /* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */
  {
      r=p;
      if(r==NULL)
     {
       printf("NO ELEMENT IN THE LIST :");
       return;
     }
       /* traverse the entire linked list */
       while(r!=NULL)
    {
      printf(" -> %d ",r->data);
      r=r->next;
    }
     printf("");
  }

int count()          //THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST
 {
   struct node *n;
   int c=0;
   n=p;
    while(n!=NULL)
   {
     n=n->next;
     c++;
   }
  return(c);
 }

void reverse(struct node *q)         //THIS FUNCTION REVERSES A LINKED LIST
{
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   p=n;
}
void bubble_sort ( int n )		/*THIS FUNCTION DOES SORTING OF THE LIST */
{
int i, j, k, temp ;
struct node *s, *q ;
k = n ;
for ( i = 0 ; i < n - 1 ; i++, k-- )
{
s = p ;
q = s-> next ;
for ( j = 1 ; j < k ; j++ )
{
if ( s-> data > q-> data )
{
temp = s-> data ;
s-> data = q-> data ;
q-> data = temp ;
}
s = s-> next ;
q = q-> next ;
}
}
}
 void index_disp(int num)                /*THIS FUNCTION DISPLAYS THE INDEX OF FIRST OCCURENCE OF ELEMENT */
 {
	int i = 1;
     struct node *temp;
	temp=p;
      while(temp!=NULL)
    { 
       if(temp->data==num)
       {
           if(temp==p)
           {i=1;
            break;
	   
           }
           else
         {
          break;
         }
      }
	else
        {
          temp= temp->next;
	i= i+1;
        }

}
if (i > count())
{printf("element not present in the list");}
else 
{printf("\nthe index of the element is %d",i);}
}
void num_ele(int num)                /*THIS FUNCTION DISPLAYS THE NUMBER OF OCCURENCE OF ELEMENT */
 {
struct node *temp;
int cnt=0,flag=0;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
cnt=cnt+1;
temp=temp->next;
}
if (flag=1)
printf("\nThe element appeared %d times in the list...", cnt);
else
printf("\nelement not present in the list");
}


  int main()       /* THIS IS THE MAIN PROGRAM  */
 {
	
        int i;
   p=NULL;
   while(1)
 {
    system("clear");
    printf("\n1.CREATING A NEW NODE OR INSERT A NUMBER AT LAST(APPEND) :");
    printf("\n2.INSERT A NUMBER AT BEGINNING(INSERT) :");
    printf("\n3.INSERT A NUMBER AT A PARTICULAR LOCATION IN lIST(INSERT) :");
    printf("\n4.PRINT THE ELEMENTS NUMBER OF ELEMENTS IN THE LIST :");
    printf("\n5.PRINT THE NUMBER OF ELEMENTS IN THE LIST :");
    printf("\n6.DELETE A NODE IN THE LINKED LIST(REMOVE) :");
    printf("\n7.REVERSE A LINKED LIST(REVERSE) :");
    printf("\n8.SORT A LINKED LIST(SORT) :");
    printf("\n9.TO FIND IF GIVEN VALUE IS PRESENT OR NOT(INDEX) :");
    printf("\n10.TO PRINT NUMBER OF OCCURENCES OF GIVEN VALUE(COUNT) :");
    printf("\n11.EXIT");
    printf("\nCHOOSE ANY NUMBER");
    scanf("%d",&i); 

      switch(i)
    {
         case 1:
      {
	 int num;
         printf("PLEASE ENTER THE NUMBER TO BE INSERTED :-");
         scanf("%d",&num);
         append(num);
	 struct node *n;
         printf(" THE  ELEMENTS IN THE LIST ARE : ");
         display(n);
         break;
      }
          case 2:
       {
	int num;
        printf("PLEASE ENTER THE NUMBER TO BE INSERTED AT THE BEG :-");
        scanf("%d",&num);
        addbeg(num);
        break;
	struct node *n;
        printf(" THE  ELEMENTS IN THE LIST ARE : ");
        display(n); 
       }

   case 3:
     {
      int num, loc,k;
      printf("PLEASE ENTER THE NUMBER TO BE INSERTED AT A SPECIFIC LOCATION:-");
      scanf("%d",&num);
      printf("PLEASE ENTER THE LOCATION NUMBER :-");
      scanf("%d",&loc);
      addafter(num,loc);
      struct node *n;
      printf(" THE  ELEMENTS IN THE LIST ARE : ");
      display(n);
      break;
    }  case 4:
      {
         struct node *n;
         printf(" THE  ELEMENTS IN THE LIST ARE : ");
         display(n);
         break;
      }

      case 5:
   {
      struct node *n;
      display(n);
      int num;
      num= count();
      printf(" TOTAL NO OF ELEMENTS IN THE LIST ARE %d",num);
      break;
   } case 6:
    {
            int num;
      printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
      scanf("%d",&num);
      del_node(num);
      struct node *n;
      printf(" THE  ELEMENTS IN THE LIST ARE : ");
      display(n);
     break;
    }
   case 7:
    {
      reverse(p);
        display(p);
        break;
    }
     case 8:
    {
      int x = count();
      bubble_sort(x);
      struct node *n;
      printf(" THE  ELEMENTS IN THE LIST ARE : ");
      display(n);
      break;
    }
case 9:
    {
            int num;
      printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
      scanf("%d",&num);
      index_disp(num);
      struct node *n;
      printf(" THE  ELEMENTS IN THE LIST ARE : ");
      display(n);
     break;
    }
case 10:
    {
            int num;
      printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
      scanf("%d",&num);
      num_ele(num);
      struct node *n;
      printf(" THE  ELEMENTS IN THE LIST ARE : ");
      display(n);
     break;
    }
  case 11:
 {
  exit(0);
 }
    }/* end if switch */
 } /* end of while */
 return 0;
}/* end of main */
